package base.db.lib.util;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import base.config.lib.MSNJOBConfig;

@SuppressWarnings("all")
public class DBConnection {
	 /**
	  * 
	  * 通过读取配置文件信息获取数据库连接对象
	  * @return
	  */
	public static Connection getConnectionByConfig() {
		try {
			Connection conn = null;
//			System.out.println(MSNJOBConfig.getInstance().getDb_driverclass());
//			System.out.println(MSNJOBConfig.getInstance().getDb_jdbcurl());
//			System.out.println(MSNJOBConfig.getInstance().getDb_username());
//			System.out.println(MSNJOBConfig.getInstance().getDb_password());
//			Class.forName(MSNJOBConfig.getInstance().getDb_driverclass());
			conn = DriverManager.getConnection(MSNJOBConfig.getInstance().getDb_jdbcurl(), MSNJOBConfig.getInstance().getDb_username(), MSNJOBConfig.getInstance().getDb_password());

			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Connection getMSMSQLConnection() {
		try {
			String sDBDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
			// String sConnStr =
			// "jdbc:sqlserver://20.20.16.237:1433;database=msnjob";
			String sConnStr = "jdbc:sqlserver://ADMIN-PC\\SQLEXPRESS:1433;database=msnjob";
			//
			Connection conn = null;
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr, "sa", "sa");

			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Connection getOracleConnection() {
		try {
			String sDBDriver = "oracle.jdbc.driver.OracleDriver";
			String sConnStr = "jdbc:oracle:thin:@192.168.8.168:1521:npos";
			Connection conn = null;
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr, "npos_00001",
					"kyss_00001");
			// System.out.println("commen:" +conn.toString());
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Connection getEOSConnection() {
		try {
			String sDBDriver = "oracle.jdbc.driver.OracleDriver";
			String sConnStr = "jjdbc:oracle:thin:@192.168.8.12:1521:vpm";
			Connection conn = null;
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr, "eos", "dslr");
			// System.out.println("commen:" +conn.toString());
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Connection getConnection() {

		try {
			String sDBDriver = "oracle.jdbc.driver.OracleDriver";
			String sConnStr = "jdbc:oracle:thin:@192.168.8.12:1521:vpm";
			Connection conn = null;
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr, "gtsh", "dslr");
			// System.out.println("commen:" +conn.toString());
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Connection getMysqlConnection() {
		try {
			String sDBDriver = "com.mysql.jdbc.Driver";
			// jdbc:mysql://<hostname>[<:3306>]/<dbname>
			String sConnStr = "jdbc:mysql://192.168.8.26:3306/wfe";
			Connection conn = null;
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr, "root", "root");
			// System.out.println("commen:" +conn.toString());

			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Connection getConnection(String JNDIDatabaseName) {
		try {
			// java:comp/env/jdbc/
			// jdbc/msnjob
			Connection conn = null;
			InitialContext ic = new InitialContext();
			DataSource ds = (DataSource) ic.lookup(JNDIDatabaseName);
			conn = ds.getConnection();
			// System.out.println("jndi:" +conn.toString());
			return conn;
		} catch (Exception sex) {
			sex.printStackTrace();
		}
		return null;
	}

}
